import java.util.Random;

class Solution {
    public int[] sortArray(int[] nums) {
        quickSort(nums);
        return nums;
    }
    public void quickSort(int[] nums) {
        quick(nums, 0, nums.length - 1);
    }
    public void quick(int[] nums, int s, int e) {
        if(s >= e) return;

        int key = nums[new Random().nextInt(e - s + 1) + s];

        int[] ret = partition(nums, s, e, key);

        quick(nums, s, ret[0]);
        quick(nums, ret[1], e);
    }
    //数组分三块
    public int[] partition(int[] nums, int left, int right, int key) {
        int i = left;
        int l = left - 1;
        int r = right + 1;
        while(i < r) {
            if(nums[i] < key) {
                swap(nums, ++l, i++);
            }else if(nums[i] == key) {
                i++;
            }else {
                swap(nums, --r, i);
            }
        }
        int[] ret = {l, r};
        return ret;
    }
    public void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}